package gob.cgr.web.sgrh.managed;

import gob.cgr.web.sgrh.util.Utilitario;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

@ManagedBean
@SessionScoped
public class ImageMB {

	 public StreamedContent getImage() throws IOException {
	        FacesContext context = FacesContext.getCurrentInstance();

	        if (context.getRenderResponse()) {
	            // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
	            return new DefaultStreamedContent();
	        }
	        else {
	            // So, browser is requesting the image. Get ID value from actual request param.
	            String filename = context.getExternalContext().getRequestParameterMap().get("foto");
	            File f=new File(Utilitario.getCarpetaFoto()+ filename);
	            return new DefaultStreamedContent(new FileInputStream(f));
	        }
	    }
	

}
